var express = require('express');
var router = express.Router();
//导入lowdb,一个小型本地JSON数据库
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
//存储数据的文件
const adapter = new FileSync(__dirname + '/../data/db.json')
const db = low(adapter)
//导入shortid
const shortid = require('shortid')

/* GET home page. */
router.get('/account', function(req, res, next) {
  const accounts = db.get('account').value();
  res.render('list',{accounts:accounts})
});

router.get('/account/create', function (req, res, next) {
    res.render('create');
});

router.post('/account',(req,res) => {
    const id = shortid.generate()
    db.get('account').unshift({id,...req.body}).write()
    res.render('success',{msg:"添加成功",url:'/account'})
})

router.get('/account:id',(req,res) => {
    db.get('account').remove({ id: req.params.id }).write()
    res.render('success', { msg: "删除成功", url: '/account' })
})

module.exports = router;
